package com.guanghua.brick.html.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.guanghua.brick.html.IDataSource;
import com.guanghua.brick.html.WebControl;

public class CoreSetTag extends BodyTagSupport {
	
	//private static Log logger = LogFactory.getLog(CoreElseTag.class);
	
	private String id = null;
	private Object data = true;
	private String dataSource = null;
	private String scope = "request";
	
	public int doEndTag() throws JspException {
		
		if (this.data == null && this.dataSource != null) {
			IDataSource ds = WebControl.getDataSourceParser(pageContext);
			this.data = ds.parseDataSource(pageContext, this.dataSource, Object.class);
		}
		
		if ("request".equals(this.scope)) {
			pageContext.getRequest().setAttribute(this.id, this.data);
		} else if ("session".equals(this.scope)) {
			pageContext.getSession().setAttribute(this.id, this.data);
		} else if ("application".equals(this.scope)){
			pageContext.getServletContext().setAttribute(this.id, this.data);
		} else {
			pageContext.getRequest().setAttribute(this.id, this.data);
		}
		
		return BodyTagSupport.EVAL_PAGE;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getDataSource() {
		return dataSource;
	}

	public void setDataSource(String dataSource) {
		this.dataSource = dataSource;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	
}
